package array;

/**
 * @Classname division
 * @Author happytaohaha
 * @Date 2020/2/28
 * @Description 二分法
 */
public class DivisionFind {

    public static int findIndexByDivision(int []array, int keyValue){
        int left = 0;
        int right = array.length-1;
        int index ;
        while (true){
            index = (left + right)/2;
            if(array[index] == keyValue){
                return index;
            }
            if(left > right){
                return -1;
            }
            if(array[index] < keyValue){
                left = index+1;
            }
            if(array[index] > keyValue){
                right = index-1;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(findIndexByDivision(new int[]{1,2,3,4,5,6,7,8,9,10},5));
    }
}
